package com.dss.auto_deploy.util;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

/**
 * @Description:
 * @ClassName: Result
 * @author: dongshoushan
 * @date: 2020年06月08日 10:18
 */
@Data
@AllArgsConstructor
@Builder
@ApiModel(value = "返回结果集")
public class Result<T> implements Serializable {

    private static final String DEFAULT_SUCCESS_CODE = "0";
    private static final String DEFAULT_SUCCESS_DESC = "操作成功";

    private static final String DEFAULT_FAILED_CODE = "500";
    private static final String DEFAULT_FAILED_DESC = "操作失败";


    private boolean success;

    private String desc;


    private String code;

    @ApiModelProperty(value = "请求返回的数据")
    private T data;

    public Result() {
        this.success = true;
        this.desc = DEFAULT_SUCCESS_DESC;
        this.code = DEFAULT_SUCCESS_CODE;
    }

    public Result(boolean success) {
        this.success = success;
        this.desc = DEFAULT_FAILED_DESC;
        this.code = DEFAULT_FAILED_CODE;
    }

    public Result(T data) {
        this();
        this.data = data;
    }

    public Result(boolean success, String desc, String code) {
        this.success = success;
        this.desc = desc;
        this.code = code;
    }

    public static <T> Result<T> success() {
        return new Result<>();
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(data);
    }

    public static <T> Result<T> fail() {
        return new Result<>(false);
    }

    public static <T> Result<T> fail(String desc) {
        return new Result<>(false, desc, DEFAULT_FAILED_CODE);
    }

    public static <T> Result<T> fail(String desc, String code) {
        return new Result<>(false, desc, code);
    }

}
